﻿using RXY_Mall.Application.Goods;
using RXY_Mall.Application.Goods.Dto;
using RXY_Mall.Application.Shop;
using RXY_Mall.Application.Statistics;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Web;
using System.Web.Mvc;

namespace RXY_Mall.Admin.Controllers
{
    public class GoodsController : BaseController
    {
        private IGoodsService _goodsService;
        private IShopService _shopService;
        private IStatisticsService _statisticsService;
        public GoodsController()
        {
            _goodsService = new GoodsService();
            _shopService = new ShopService();
            _statisticsService = new StatisticsService();
        }

        #region 商品
        

        /// <summary>
        /// 商品
        /// </summary>
        /// <param name="id">商品类型</param>
        /// <returns></returns>
        public ActionResult List(int id = 1)
        {
            ViewBag.tid = id;
            ViewBag.tName = "普通";
            //switch (id)
            //{
            //    case 2:
            //        ViewBag.tName = "合力免单";
            //        break;
            //    case 3:
            //        ViewBag.tName = "趣卡专享";
            //        break;
            //    case 4:
            //        ViewBag.tName = "1元购";
            //        break;
            //}
            var statistics = _statisticsService.GetGoodsStatistics();
            if (statistics == null)
            {
                ViewBag.goods_total = 0;
                ViewBag.goods1_total = 0;
                ViewBag.goods2_total = 0;
                ViewBag.goods3_total = 0;
                ViewBag.goods_publish = 0;
                ViewBag.goods_no_stock = 0;
                ViewBag.goods_no_publish = 0;
            }
            else
            {
                ViewBag.goods_total = statistics.goods_total;
                ViewBag.goods1_total = statistics.goods1_total;
                ViewBag.goods2_total = statistics.goods2_total;
                ViewBag.goods3_total = statistics.goods3_total;
                ViewBag.goods_publish = statistics.goods_publish;
                ViewBag.goods_no_stock = statistics.goods_no_stock;
                ViewBag.goods_no_publish = statistics.goods_no_publish;
            }
            return View();
        }

        public JsonResult GoodsList(string sEcho, int iDisplayStart, int iDisplayLength, string dateMin, string dateMax, string title, string shopName, string code, string category, int status, int shopId = -1, int categoryId = -1, int type = 1)
        {
            int totalCount = 0;
            var list = _goodsService.GetAllGoodsByPage(iDisplayStart, iDisplayLength, dateMin, dateMax, title, shopName, code, category ,categoryId, shopId, status, type, out totalCount);
            return Json(new { sEcho = sEcho, iTotalRecords = totalCount, iTotalDisplayRecords = totalCount, aaData = list }, JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// 商品
        /// </summary>
        /// <param name="id">商品类型 1出售中，2售罄， 0下架</param>
        /// <returns></returns>
        public ActionResult GoodsListBySale(int id = 0)
        {
            ViewBag.tid = id;
            ViewBag.tName = "";
            switch (id)
            {
                case 1:
                    ViewBag.tName = "出售中";
                    break;
                case 2:
                    ViewBag.tName = "已售罄";
                    break;
                case 3:
                    ViewBag.tName = "已下架";
                    break;
            }
            
            var statistics = _statisticsService.GetGoodsStatistics();
            if (statistics == null)
            {
                ViewBag.goods_total = 0;
                ViewBag.goods1_total = 0;
                ViewBag.goods2_total = 0;
                ViewBag.goods3_total = 0;
                ViewBag.goods_publish = 0;
                ViewBag.goods_no_stock = 0;
                ViewBag.goods_no_publish = 0;
            }
            else
            {
                ViewBag.goods_total = statistics.goods_total;
                ViewBag.goods1_total = statistics.goods1_total;
                ViewBag.goods2_total = statistics.goods2_total;
                ViewBag.goods3_total = statistics.goods3_total;
                ViewBag.goods_publish = statistics.goods_publish;
                ViewBag.goods_no_stock = statistics.goods_no_stock;
                ViewBag.goods_no_publish = statistics.goods_no_publish;
            }
            return View();
        }

        public JsonResult GetGoodsListBySale(string sEcho, int iDisplayStart, int iDisplayLength, string dateMin, string dateMax, string title, string shopName, string code, string category, int status, int goodsType, int shopId = -1, int categoryId = -1, int type = -1)
        {
            int totalCount = 0;
            var list = _goodsService.GetGoodsByPage(iDisplayStart, iDisplayLength, dateMin, dateMax, title, shopName, code, category, categoryId, shopId, status, goodsType, type, out totalCount);
            return Json(new { sEcho = sEcho, iTotalRecords = totalCount, iTotalDisplayRecords = totalCount, aaData = list }, JsonRequestBehavior.AllowGet);
        }

        public JsonResult GetServiceList()
        {
            return Json(_goodsService.GetGoodsServiceList(), JsonRequestBehavior.AllowGet);
        }
        public ActionResult EditGoods(int id = 0)
        {
            GoodsInput model = new GoodsInput();
            if (id > 0)
            {
                model = _goodsService.GetGoodsById(id);
            }
            ViewBag.NowTime = DateTime.Now.ToString("yyyy-MM-dd");
            return View(model);
        }

        [HttpPost]
        public async Task<JsonResult> AddorEditGoodsInfo(GoodsInput input)
        {
            var result = await _goodsService.AddorEditGoods(MemberId, input);
            if (result.code == 1)
            {
                return Json(new { status = "y", info = result.data,type=result.msg });
            }
            else
            {
                return Json(new { status = "n", info = result.msg });
            }
        }

        //上架/下架
        [HttpPost]
        public async Task<JsonResult> SetGoodsIsUse()
        {
            string id = Request.Form["id"];
            string isUse = Request.Form["isUse"];
            int idDefault = 0;
            int isUseDefault = 0;
            if (string.IsNullOrWhiteSpace(id) || !int.TryParse(id, out idDefault))
            {
                return Json(new { status = "n", info = "ID错误" });
            }
            if (string.IsNullOrWhiteSpace(isUse) || !int.TryParse(isUse, out isUseDefault))
            {
                return Json(new { status = "n", info = "参数错误" });
            }

            var result = await _goodsService.SetGoodsIsUse(MemberId, idDefault, isUseDefault);
            if (result.code==1)
            {
                return Json(new { status = "y", info = "操作成功。" });
            }
            else
            {
                return Json(new { status = "n", info =result.msg });
            }
        }
        //新品
        [HttpPost]
        public ActionResult SetGoodsIsNew()
        {
            string id = Request.Form["id"];
            string isUse = Request.Form["isUse"];
            int idDefault = 0;
            int isUseDefault = 0;
            if (string.IsNullOrWhiteSpace(id) || !int.TryParse(id, out idDefault))
            {
                return Json(new { status = "n", info = "ID错误" });
            }
            if (string.IsNullOrWhiteSpace(isUse) || !int.TryParse(isUse, out isUseDefault))
            {
                return Json(new { status = "n", info = "参数错误" });
            }

            var result = _goodsService.SetGoodsIsNew(MemberId, idDefault, isUseDefault);
            if (result)
            {
                return Json(new { status = "y", info = "操作成功。" });
            }
            else
            {
                return Json(new { status = "n", info = "操作失败。" });
            }
        }
        //置顶
        [HttpPost]
        public ActionResult SetGoodsIsTop()
        {
            string id = Request.Form["id"];
            string isUse = Request.Form["isUse"];
            int idDefault = 0;
            int isUseDefault = 0;
            if (string.IsNullOrWhiteSpace(id) || !int.TryParse(id, out idDefault))
            {
                return Json(new { status = "n", info = "ID错误" });
            }
            if (string.IsNullOrWhiteSpace(isUse) || !int.TryParse(isUse, out isUseDefault))
            {
                return Json(new { status = "n", info = "参数错误" });
            }

            var result = _goodsService.SetGoodsIsTop(MemberId, idDefault, isUseDefault);
            if (result)
            {
                return Json(new { status = "y", info = "操作成功。" });
            }
            else
            {
                return Json(new { status = "n", info = "操作失败。" });
            }
        }
        #endregion

        #region 商品图片
        public ActionResult GoodsPictures(int id=0, int tid=0)
        {
            ViewBag.id = id;
            //var model= _goodsService.GetGoodsById(id);
            ViewBag.tid = tid;
            ViewBag.goods_tid =1;
            //if (model!=null)
            //{
            //    ViewBag.goods_tid = model.type_id;
            //}
            //else
            //{
            //    ViewBag.id = 0;
            //}
            return View();
        }
        public JsonResult GetGoodsPictures(int id,int tid)
        {
            var img_list = _goodsService.GetGoodsPictures(id, tid);
            return Json(img_list);
        }
        [HttpPost]
        public async Task<JsonResult> SaveGoodsPictures(GoodsPictureInput input)
        {
            var result = await _goodsService.AddorEditGoodsPictures(MemberId, input);
            if (result.code == 1)
            {
                return Json(new { status = "y", info = "保存成功。", sId = result.data });
            }
            else
            {
                return Json(new { status = "n", info = result.msg });
            }
        }
        #endregion

        #region 商品规格
        public ActionResult SetGoodsSpec(int id ,int tid=1)
        {
            ViewBag.id = id;
            ViewBag.tid = _goodsService.GetGoodsType(id);
            ViewBag.skuCount = _goodsService.GoodsSkuCount(id);
            return View();
        }
        [HttpPost]
        public async Task<JsonResult> AddMySpec(int goodsId, string name, string description)
        {
            var result = await _goodsService.AddSpec(MemberId, goodsId, name, description);
            if (result.code == 1)
            {
                return Json(new { status = "y", info = "保存成功。", sId = result.data });
            }
            else
            {
                return Json(new { status = "n", info = result.msg });
            }
        }
        [HttpPost]
        public async Task<JsonResult> AddSpecValue(int id,int sId, string name,string img)
        {
            var result = await _goodsService.AddSpecValue(MemberId, id,sId,name,img);
            if (result.code == 1)
            {
                return Json(new { status = "y", info = "保存成功。", sId = result.data });
            }
            else
            {
                return Json(new { status = "n", info = result.msg });
            }
        }

        public JsonResult GetSpecWithValue(int id)
        {
            var list = _goodsService.GetSpecWithValue(id);
            return Json(list);
        }

        //public JsonResult GetGoodsSpec(int id)
        //{
        //    var list = _goodsService.GetGoodsSpec(id);
        //    return Json(list);
        //}

        //[HttpPost]
        //public async Task<JsonResult> DeleteGoodsSpec(int goodsId, int specId)
        //{
        //    var result = await _goodsService.DeleteGoodsSpec(MemberId, goodsId, specId);
        //    if (result.code == 1)
        //    {
        //        return Json(new { status = "y", info = "操作成功", sId = result.data });
        //    }
        //    else
        //    {
        //        return Json(new { status = "n", info = result.msg });
        //    }
        //}
        //public ActionResult AddGoodsSpec(int id)
        //{
        //    ViewBag.goodsId = id;
        //    return View();
        //}

        //public ActionResult SelectGoodsSpec(int id)
        //{
        //    ViewBag.goodsId = id;
        //    return View();
        //}
        //public JsonResult GetSpecList()
        //{
        //    var list = _goodsService.GetSpec();
        //    return Json(list);
        //}
        //[HttpPost]
        //public async Task<JsonResult> SelectSpec(int goodsId, int specId)
        //{
        //    var result = await _goodsService.AddGoodsSpec(MemberId, goodsId, specId);
        //    if (result.code == 1)
        //    {
        //        return Json(new { status = "y", info = "保存成功。", sId = result.data });
        //    }
        //    else
        //    {
        //        return Json(new { status = "n", info = result.msg });
        //    }
        //}
        //[HttpPost]
        //public async Task<JsonResult> AddSpec(int goodsId, string name, string description)
        //{
        //    var result = await _goodsService.AddGoodsSpec(MemberId, goodsId, name, description);
        //    if (result.code == 1)
        //    {
        //        return Json(new { status = "y", info = "保存成功。", sId = result.data });
        //    }
        //    else
        //    {
        //        return Json(new { status = "n", info = result.msg });
        //    }
        //}
        #endregion

        #region 商品规格0
        public ActionResult GoodsSpec(int id)
        {
            ViewBag.id = id;
            return View();
        }

        public JsonResult GetGoodsSpec(int id)
        {
            var list = _goodsService.GetGoodsSpec(id);
            return Json(list);
        }

        [HttpPost]
        public async Task<JsonResult> DeleteGoodsSpec(int goodsId, int specId)
        {
            var result = await _goodsService.DeleteGoodsSpec(MemberId, goodsId, specId);
            if (result.code == 1)
            {
                return Json(new { status = "y", info = "操作成功", sId = result.data });
            }
            else
            {
                return Json(new { status = "n", info = result.msg });
            }
        }
        public ActionResult AddGoodsSpec(int id)
        {
            ViewBag.goodsId = id;
            return View();
        }

        public ActionResult SelectGoodsSpec(int id)
        {
            ViewBag.goodsId = id;
            return View();
        }
        public JsonResult GetSpecList()
        {
            var list = _goodsService.GetSpec();
            return Json(list);
        }
        [HttpPost]
        public async Task<JsonResult> SelectSpec(int goodsId, int specId)
        {
            var result = await _goodsService.AddGoodsSpec(MemberId, goodsId, specId);
            if (result.code == 1)
            {
                return Json(new { status = "y", info = "保存成功。", sId = result.data });
            }
            else
            {
                return Json(new { status = "n", info = result.msg });
            }
        }
        [HttpPost]
        public async Task<JsonResult> AddSpec(int goodsId, string name, string description)
        {
            var result = await _goodsService.AddGoodsSpec(MemberId, goodsId, name, description);
            if (result.code == 1)
            {
                return Json(new { status = "y", info = "保存成功。", sId = result.data });
            }
            else
            {
                return Json(new { status = "n", info = result.msg });
            }
        }
        #endregion

        #region 商品规格值0
        /// <summary>
        /// 设置规格值
        /// </summary>
        /// <param name="id">商品ID</param>
        /// <param name="tid">规格ID</param>
        /// <returns></returns>
        public ActionResult GoodsSpecValue(int id, int tid)
        {
            ViewBag.id = id;
            ViewBag.tid = tid;
            return View();
        }

        public JsonResult GetGoodsSpecValue(int id, int tid)
        {
            var list = _goodsService.GetGoodsSpecValue(id, tid);
            return Json(list);
        }

        /// <summary>
        /// 添加规格值
        /// </summary>
        /// <param name="id">商品ID</param>
        /// <param name="tid">规格ID</param>
        /// <returns></returns>
        public ActionResult AddGoodsSpecValue(int id, int tid=0)
        {
            ViewBag.id = id;
            ViewBag.tid = tid;
            return View();
        }

        [HttpGet]
        public JsonResult GoodsSpecValueInfo(int id = 0)
        {
            if (id > 0)
            {
                var model = _goodsService.GetGoodsSpecValueById(id);
                return Json(model, JsonRequestBehavior.AllowGet);
            }
            return null;
        }

        [HttpPost]
        public async Task<JsonResult> SaveGoodsSpecValue(GoodsSpecValueInput input)
        {
            var result = await _goodsService.AddorEditGoodsSpecValue(MemberId, input);
            if (result.code == 1)
            {
                return Json(new { status = "y", info = "保存成功。", sId = result.data });
            }
            else
            {
                return Json(new { status = "n", info = result.msg });
            }
        }

        [HttpPost]
        public async Task<JsonResult> DeleteGoodsSpecValue(int id,int tid)
        {
            var result = await _goodsService.DeleteGoodsSpecValue(id, tid);
            if (result.code == 1)
            {
                return Json(new { status = "y", info = "操作成功", sId = result.data });
            }
            else
            {
                return Json(new { status = "n", info = result.msg });
            }
        }
        #endregion

        #region 商品SKU
        [HttpPost]
        public async Task<JsonResult> MakeSku(int id)
        {
            var result = await _goodsService.MakeSku(id);
            if (result.code == 1)
            {
                return Json(new { status = "y", info = "操作成功", sId = result.data });
            }
            else
            {
                return Json(new { status = "n", info = result.msg });
            }
        }

        public ActionResult GoodsSku(int id)
        {
            ViewBag.id = id;
            ViewBag.tid = _goodsService.GetGoodsType(id);
            ViewBag.skuCount = _goodsService.GoodsSkuCount(id);
            return View();
        }

        public ActionResult EditGoddsSku(int id=0)
        {
            ViewBag.id = id;
            GoodsSkuDto model = new GoodsSkuDto();
            if (id > 0)
            {
                model = _goodsService.GetGoodsSkueById(id);
            }
            return View(model);
        }

        public JsonResult GetGoodsSkuList(int id=0)
        {
            var list = _goodsService.GetGoodsSkuList(id);
            return Json(list);
        }

        public JsonResult GetSpecSkuList(int id = 0)
        {
            var list = _goodsService.GetGoodsSpecBySkuCount(id);
            return Json(list,JsonRequestBehavior.AllowGet);
        }

        [HttpPost]
        public async Task<JsonResult> SaveGoodsSku(GoodsSkuInput input)
        {
            var result = await _goodsService.EditGoodsSku(MemberId,input);
            if (result.code == 1)
            {
                return Json(new { status = "y", info = "操作成功", all = result.data });
            }
            else
            {
                return Json(new { status = "n", info = result.msg });
            }
        }

        [HttpPost]
        public async Task<JsonResult> DeleteSku(int id)
        {
            var result = await _goodsService.DeleteSku(id);
            if (result.code == 1)
            {
                return Json(new { status = "y", info = "操作成功", sId = result.data });
            }
            else
            {
                return Json(new { status = "n", info = result.msg });
            }
        }

        [HttpPost]
        public ActionResult SetGoodsSKUPublish()
        {
            string id = Request.Form["id"];
            string isUse = Request.Form["isUse"];
            int idDefault = 0;
            int isUseDefault = 0;
            if (string.IsNullOrWhiteSpace(id) || !int.TryParse(id, out idDefault))
            {
                return Json(new { status = "n", info = "ID错误" });
            }
            if (string.IsNullOrWhiteSpace(isUse) || !int.TryParse(isUse, out isUseDefault))
            {
                return Json(new { status = "n", info = "参数错误" });
            }

            var result = _goodsService.SetGoodsSkuIsUse(MemberId, idDefault, isUseDefault);
            if (result)
            {
                return Json(new { status = "y", info = "操作成功。" });
            }
            else
            {
                return Json(new { status = "n", info = "操作失败。" });
            }
        }

        [HttpPost]
        public async Task<JsonResult> BulkUpdate(int id, string number,int type)
        {
            var result = await _goodsService.BulkUpdateSku(MemberId, id,number,type);
            if (result.code == 1)
            {
                return Json(new { status = "y", info = "操作成功" });
            }
            else
            {
                return Json(new { status = "n", info = result.msg });
            }
        }
        #endregion

        #region 商品评论
        public ActionResult Comment()
        {
            return View();
        }

        public JsonResult CommentList(int iDisplayStart, int iDisplayLength, string dateMin, string dateMax,  int status)
        {
            int totalCount = 0;
            var userList = _goodsService.GetAllCommentByPage(iDisplayStart, iDisplayLength, dateMin, dateMax, status,  out totalCount);
            return Json(new { iTotalRecords = totalCount, iTotalDisplayRecords = totalCount, aaData = userList }, JsonRequestBehavior.AllowGet);
        }

        //禁用/启用
        [HttpPost]
        public ActionResult SetCommentIsUse()
        {
            string id = Request.Form["id"];
            string isUse = Request.Form["isUse"];
            int idDefault = 0;
            int isUseDefault = 0;
            if (string.IsNullOrWhiteSpace(id) || !int.TryParse(id, out idDefault))
            {
                return Json(new { status = "n", info = "ID错误" });
            }
            if (string.IsNullOrWhiteSpace(isUse) || !int.TryParse(isUse, out isUseDefault))
            {
                return Json(new { status = "n", info = "参数错误" });
            }

            var result = _goodsService.SetCommentIsUse(MemberId, idDefault, isUseDefault);
            if (result)
            {
                return Json(new { status = "y", info = "操作成功。" });
            }
            else
            {
                return Json(new { status = "n", info = "操作失败。" });
            }
        }
        #endregion

        /// <summary>
        /// 商品
        /// </summary>
        /// <param name="id">商品类型</param>
        /// <returns></returns>
        public ActionResult GetSelectedGoods(int id = 1)
        {
            ViewBag.tid = id;
            ViewBag.tName = "普通";
            switch (id)

            {
                case 2:
                    ViewBag.tName = "合力免单";
                    break;
                case 3:
                    ViewBag.tName = "趣卡专享";
                    break;
            }
            return View();
        }
}
}